﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Interfaces.Presupuesto
{
    public partial class BuscarPresupuesto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
         
            if (!IsPostBack)
            {
                for (int gestion = 1967; gestion < 2023; gestion++)
                {
                    DropDownList1.Items.Add(gestion.ToString());
                    DropDownList2.Items.Add(gestion.ToString());
                }
                DropDownList2.SelectedIndex = DropDownList2.Items.Count - 1;
            }
            LabelError.Visible = false;
            LinkButton1.Visible = false;
        }

        protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
        {
            Panel3.Visible = RadioButton4.Checked;
            Panel4.Visible = RadioButton5.Checked;
        }

        protected void RadioButton5_CheckedChanged(object sender, EventArgs e)
        {
            Panel4.Visible = RadioButton5.Checked;
            Panel3.Visible = RadioButton4.Checked;
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
               string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (RadioButton4.Checked)
                {
                    DataList1.DataSource = Processes.Presupuesto.GestorPresupuesto.getPresupuesto(TextBox1.Text, TextBox1.Text, true, true,true);
                }
                else
                {
                    DataList1.DataSource = Processes.Presupuesto.GestorPresupuesto.getPresupuesto(DropDownList1.SelectedItem.Text, DropDownList2.SelectedItem.Text, RadioButton1.Checked, RadioButton2.Checked,true);
                }
                DataList1.DataBind();
                if (DataList1.Items.Count == 0)
                {
                    LabelError.Visible = true;
                    LabelError.Text = "No se pudo encontrar ninguna gestion con estas caracteristicas puede probar:";
                    LinkButton1.Visible = true;
                }
            }
            catch (Exception ex)
            {
                LabelError.Visible = true;
                LabelError.Text = ex.Message;
            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                Label gestion=(Label)e.Item.FindControl("Gestion");
                if(gestion!=null)
                {
                    if (e.CommandName == "Ver")
                    {

                        Response.Redirect("Ver.aspx?Gestion=" + gestion.Text);
                    }
                    else if (e.CommandName == "Editar")
                    {
                        Response.Redirect("FormularioPresupuesto.aspx?Gestion=" + gestion.Text + "&Tipo=I");

                    }
                    else
                    {
                        Processes.Presupuesto.GestorPresupuesto.CerrarPresupuesto(gestion.Text);
                        LinkButton editar = (LinkButton)e.Item.FindControl("Editar");
                        LinkButton cerrar =(LinkButton) e.Item.FindControl("CerrarGestion");
                        Label estado =(Label)e.Item.FindControl("Estado");
                        editar.Visible = false;
                        cerrar.Visible = false;
                        estado.Text = "Cerrado";
                    }
                }
            }
            catch (Exception ex)
            {
                LabelError.Visible = true;
                LabelError.Text = ex.Message;
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("CrearPresupuesto.aspx");
        }

 
    }
}